package com.shushan.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.andview.refreshview.callback.IHeaderCallBack;
import com.shushan.shushanbao.R;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: RefreshHeader
 * @Desciption: 下拉刷新自定义头
 * @author: yichaohua
 * @date: 2018-08-06
 */
public class RefreshHeader extends LinearLayout implements IHeaderCallBack {

    private static final String TAG = "RefreshHeader";

    Context context;

    private ImageView mArrowImageView;
    private ProgressBar mProgressBar;
    private TextView mHintTextView;
    private TextView timeTextView;

    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;
    private final int ROTATE_ANIM_DURATION = 180;


    public RefreshHeader(Context context) {
        super(context);
        this.context = context;

        initView(context);
    }

    public RefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.refresh_header, this);

        mArrowImageView = (ImageView)findViewById(com.shushan.app_frame.R.id.refresh_header_arrow);
        mHintTextView = (TextView)findViewById(com.shushan.app_frame.R.id.refresh_header_hint_textview);
        timeTextView = (TextView)findViewById(com.shushan.app_frame.R.id.refresh_header_time);
        timeTextView.setText(new SimpleDateFormat("MM-dd HH:mm").format(new Date()));
        mProgressBar = (ProgressBar)findViewById(com.shushan.app_frame.R.id.refresh_header_progressbar);

        mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }

    @Override
    public void onStateNormal() {
        mArrowImageView.clearAnimation();
        mArrowImageView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        mHintTextView.setText(R.string.refresh_header_hint_normal);
    }

    @Override
    public void onStateReady() {
        mArrowImageView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        mArrowImageView.clearAnimation();
        mArrowImageView.startAnimation(mRotateUpAnim);
        mHintTextView.setText(R.string.refresh_header_hint_ready);

    }

    @Override
    public void onStateRefreshing() {
        mArrowImageView.clearAnimation();
        mArrowImageView.setVisibility(View.INVISIBLE);
        mProgressBar.setVisibility(View.VISIBLE);
        mHintTextView.setText(R.string.refresh_header_hint_loading);

    }

    @Override
    public void onStateFinish(boolean success) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY, int deltaY) {

    }

    @Override
    public void setRefreshTime(long lastRefreshTime) {
        timeTextView.setText(new SimpleDateFormat("MM-dd HH:mm").format(new Date()));
    }

    @Override
    public void hide() {
        setVisibility(View.GONE);
    }

    @Override
    public void show() {
        setVisibility(View.VISIBLE);
    }

    @Override
    public int getHeaderHeight() {
        return getMeasuredHeight();
    }
}
